<?php
/**
 * Created by PhpStorm.
 * User: hoter.zhang
 * Date: 2016/5/24
 * Time: 17:14
 */

namespace app\core\components;

class StringHelper extends \yii\helpers\StringHelper
{

    /**
     * 转换字节数为其他单位
     * @param string $filesize 字节大小
     * @return string 返回大小
     */
    public static function sizeCount($filesize){
        if ($filesize >= 1073741824) {
            $filesize = round($filesize / 1073741824 * 100) / 100 . ' GB';
        } elseif ($filesize >= 1048576) {
            $filesize = round($filesize / 1048576 * 100) / 100 . ' MB';
        } elseif ($filesize >= 1024) {
            $filesize = round($filesize / 1024 * 100) / 100 . ' KB';
        } else {
            $filesize = $filesize . ' Bytes';
        }
        return $filesize;
    }

    public static function guid($namespace=""){
        static $guid = '';
        $uid = uniqid("", true);
        $data = $namespace;
        $data .= $_SERVER['REQUEST_TIME'];
        $data .= $_SERVER['HTTP_USER_AGENT'];
//        $data .= $_SERVER['LOCAL_ADDR'];
//        $data .= $_SERVER['LOCAL_PORT'];
        $data .= $_SERVER['REMOTE_ADDR'];
        $data .= $_SERVER['REMOTE_PORT'];
        $hash = strtoupper(hash('ripemd128', $uid . $guid . md5($data)));
        // test str combination
//        $guid =
//            substr($hash, 0, 6) .
//            '-' .
//            substr($hash, 8, 4) .
//            '-' .
//            substr($hash, 12, 4) .
//            '-' .
//            substr($hash, 16, 4) .
//            '-' .
//            substr($hash, 20, 8);
        return $hash;
    }

    public static function guid2(){
        if (function_exists('com_create_guid')){
            return com_create_guid();
        }else{
            mt_srand((double)microtime()*10000);//optional for php 4.2.0 and up.
            $charid = strtoupper(md5(uniqid(rand(), true)));
            $hyphen = chr(45);// "-"
            $uuid = chr(123)// "{"
                .substr($charid, 0, 8).$hyphen
                .substr($charid, 8, 4).$hyphen
                .substr($charid,12, 4).$hyphen
                .substr($charid,16, 4).$hyphen
                .substr($charid,20,12)
                .chr(125);// "}"
            return $uuid;
        }
    }

//    public static function generateCode(){
//        return \Yii::$app->security->generateRandomString(8);
//    }

    public static function str_cut($str){
        $strlen = mb_strlen($str, 'utf-8');
        $key = mb_substr($str, 0, 10, 'utf-8');
        return $key . str_repeat("*", $strlen);
    }

    public static function truncate_utf8_string($string, $length, $etc = '...')
    {
        $result = '';
        $string = html_entity_decode(trim(strip_tags($string)), ENT_QUOTES, 'UTF-8');
        $strlen = strlen($string);
        for ($i = 0; (($i < $strlen) && ($length > 0)); $i++)
        {
            if ($number = strpos(str_pad(decbin(ord(substr($string, $i, 1))), 8, '0', STR_PAD_LEFT), '0'))
            {
                if ($length < 1.0)
                {
                    break;
                }
                $result .= substr($string, $i, $number);
                $length -= 1.0;
                $i += $number - 1;
            }
            else
            {
                $result .= substr($string, $i, 1);
                $length -= 0.5;
            }
        }
        $result = htmlspecialchars($result, ENT_QUOTES, 'UTF-8');
        if ($i < $strlen)
        {
            $result .= $etc;
        }
        return $result;
    }

    public static function getAgent(){
        $agent = strtolower($_SERVER['HTTP_USER_AGENT']);
        if(strpos($agent, 'iphone') || strpos($agent, 'ipad')) {
            return 'ios';
        } elseif (strpos($agent, 'android')) {
            return 'android';
        } else {
            return 'pc';
        }
    }

    public static function getAgentForID(){
        $agent = strtolower($_SERVER['HTTP_USER_AGENT']);
        if(strpos($agent, 'iphone') || strpos($agent, 'ipad')) {
            return 1;
        } elseif (strpos($agent, 'android')) {
            return 2;
        } else {
            return 3;
        }
    }

    public static function getKeywords($title='', $content='', $encode='utf-8') {
        if ($title == '') {
            return false;
        }
        $title = rawurlencode(strip_tags($title));
        $content = strip_tags($content);
        if (strlen($content) > 2400) {
            $content = mb_substr($content, 0, 800, $encode);
        }
        $content = rawurlencode($content);
        $url = 'http://keyword.discuz.com/related_kw.html?title='.$title.'&content='.$content.'&ics='.$encode.'&ocs='.$encode;
        $response = simplexml_load_file($url);

        $result = $response->keyword->result;

        $data = [];
        foreach ($result->item as $key=>$value) {
            array_push($data, (string)$value->kw);
        }
        if (count($data) > 0) {
            return $data;
        } else {
            return false;
        }
    }

    public static function limit_words($string, $word_limit)
    {
        $words = explode(" ",$string);
        return implode(" ",array_splice($words,0,$word_limit));
    }
}